<?php
class DigCms_Model_ItemNode
{
    const NAME = 'item_nodes';
    
    /**
     * gets all of the nodes for the specified item
     *
     * @param unknown_type $itemId
     * @return zend_db_table_rowset
     */
    public static function getNodes($itemId)
    {
        $nodeModel = Dig_Service_Factory::model(self::NAME);
        $select = $nodeModel->select();
        $select->where('item_id = ?', $itemId);
        $results = $nodeModel->fetchAll($select);
        if($results->count() > 0) {
            $resultArray = array();
            foreach($results as $row) {
                $resultArray[$row->node] = self::getContent($row);
            }
            return $resultArray;
        }
    }
    
    /**
     * returns a specific node's content
     *
     * @param unknown_type $itemId
     * @param unknown_type $node
     * @return unknown
     */
    public static function getNode($itemId, $node, $asRow = false)
    {
        $nodeModel = Dig_Service_Factory::model(self::NAME);
        $select = $nodeModel->select();
        $select->where('item_id = ?', $itemId);
        $select->where('node = ?', $node);
        $row = $nodeModel->fetchRow($select);
        if($row) {
            if($asRow) {
                return $row;
            }else{
                return self::getContent($row);
            }
        }
    }
    
    /**
     * creates a new instance of the appropriate item if this node is a reference to an item
     * otherwise it returns the content
     *
     * @param Zend_Db_Table_Row $row
     * @return mixed
     */
    public static function getContent(Zend_Db_Table_Row $row)
    {
        if(!empty($row->related_item)) {
            return DigCms_Model_Item::factory($row->content);
        } else {
            return $row->content;
        }
    }
    
    /**
     * finds items which have the specified node set to the value
     *
     * @param string $node
     * @param string $value
     */
    public static function seek($node, $value)
    {
        //todo: create seek method
    }
    
    /**
     * removes the specified node from the item
     *
     * @param int $itemId
     * @param varchar $node
     * @return int, number of nodes removed
     */
    public static function removeNode($itemId, $node)
    {
        $nodeModel = Dig_Service_Factory::model(self::NAME);
        $where = array(
            'item_id' => $itemId,
            'node'		=> $node
        );
        return $nodeModel->delete($where);
    }
    
    /**
     * removes all of the nodes from the item
     *
     * @param int $itemId
     * @return int, number of nodes removed
     */
    public static function removeNodes($itemId)
    {
        $nodeModel = Dig_Service_Factory::model(self::NAME);
        return $nodeModel->delete('item_id = ' . $itemId);
    }
    
    /**
     * sets the specified node
     * you can alternatively pass a DigCms_Model_Item instance as the content
     * it will relate the rows in that case
     *
     * @param int $itemId
     * @param string $node
     * @param mixed $content
     */
    public static function setNode($itemId, $node, $content)
    {
        $nodeRow = self::getNode($itemId, $node, true);
        if(!$nodeRow) {
            $nodeModel = Dig_Service_Factory::model(self::NAME);
            $nodeRow = $nodeModel->createRow();
            $nodeRow->item_id = $itemId;
            $nodeRow->node = $node;
        }
        // if you pass the content as an item it will save a reference to the item
        if($content instanceof DigCms_Model_Item) {
            $nodeRow->related_item = $content->id;
            $nodeRow->content = null;
        } else {
            $nodeRow->related_item = null;
            $nodeRow->content = $content;
        }
        return $nodeRow->save();
    }
}
?>